package com.goldsign.pay.common.core.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.goldsign.pay.common.core.dao.CrudDao;
import com.goldsign.pay.common.core.entity.DataEntity;
import com.goldsign.pay.common.core.persistence.enums.GenReasonEnum;
import com.goldsign.pay.common.core.service.base.BaseService;

@Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDao<T>,T extends DataEntity<T>>  extends BaseService{
	
	@Autowired
	protected D dao;
	
	/**
	 * 获取单条数据
	 * 
	 * @param id
	 * @return
	 */
	public T get(String id) {
		return dao.get(id);
	}

	/**
	 * 获取单条数据
	 * 
	 * @param entity
	 * @return
	 */
	public T get(T entity) {
		return dao.get(entity);
	}

	/**
	 * 查询列表数据
	 * 
	 * @param entity
	 * @return
	 */
	public List<T> findList(T entity) {
		return dao.findList(entity);
	}

	/**
//	 * 查询分页数据
//	 * 
//	 * @param page
//	 *            分页对象
//	 * @param entity
//	 * @return
//	 */
//	public Page<T> findPage(Page<T> page, T entity) {
//		entity.setPage(page);
//		page.setList(dao.findList(entity));
//		return page;
//	}

	/**
	 * 保存数据（插入或更新）
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public int save(T entity) {
		int cnt = 0 ;
		if (entity.isNewRecord()) {
			entity.preInsert();
			cnt = dao.insert(entity);
		} else {
			entity.preUpdate();
			cnt = dao.update(entity);
		}
		return cnt;
	}

	@Transactional(readOnly = false)
	public void saveWithBackup(T entity) {
		if (entity.isNewRecord()) {
			entity.preInsert();
			dao.insert(entity);
		} else {
			entity.preUpdateWithBackup(GenReasonEnum.UPDATE);
			dao.backup(entity);
			dao.update(entity);
		}
	}

	/**
	 * 时间查询，日期+24H
	 */
	@Transactional(readOnly = false)
	public Date findByEndTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, +23);
		calendar.add(Calendar.MINUTE, +59);
		calendar.add(Calendar.SECOND, +59);
		return calendar.getTime();
	}

	/**
	 * 删除数据
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public int delete(T entity) {
		entity.setDelflag(1);
		return dao.delete(entity);
	}

	@Transactional(readOnly = false)
	public int deleteWithBackup(T entity) {
		entity.preUpdateWithBackup(GenReasonEnum.DELETE);
		dao.backup(entity);
		return dao.delete(entity);
	}

	/**
	 * 启用/停用数据
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void updateWithStatus(T entity) {
		entity.preUpdateWithBackup(GenReasonEnum.UPDATE);
		dao.backup(entity);
		dao.updateStatus(entity);
	}


	public T getBySample(T sample) {
		return dao.getBySample(sample);
	}
	

//	public List<T> findValidList(BaseEntity<?> target, T sample) {
//		sample.setState(Global.STATE_AUDITED);
//		if (target.isFilterCity()) {
//			sample.setCityCode(target.getCityCode());
//			sample.setCardOfficeCode(target.getCardOfficeCode());
//		}
//		return dao.findList(sample);
//	}
	
	
}
